import paramiko
import pathlib
import zipfile
from ShapeAndRasterToolBox import UFP
import shutil
# from UFP import ZipFile


class SSHConnection:
    """
    用于连接远程服务器
    """

    def __init__(self, host, port, username, password):
        """
        连接对象初始化
        :param host: 
        :param port: 
        :param username: 
        :param password: 
        """""
        self._host = host
        self._port = port
        self._username = username
        self._password = password
        self._transport = None
        self._sftp = None
        self._client = None
        self._connect()

    def _connect(self):
        """
        创建链接
        :return:
        """
        transport = paramiko.Transport((self._host, self._port))
        transport.connect(username=self._username, password=self._password)
        self._transport = transport
        return

    def Download(self, remotefilepath, localfilepath):
        """
        下载
        :param remotefilepath: 远程服务器的文件路径
        :param localfilepath: 本地主机的文件路径
        :return:
        """
        if self._sftp is None:
            self._sftp = paramiko.SFTPClient.from_transport(self._transport)
        self._sftp.get(remotefilepath, localfilepath)
        return

    def Upload(self, localfilepath, remotefilepath):
        """
        上传文件
        :param localfilepath: 本地主机文件路径
        :param remotefilepath: 远程主机文件路径
        :return:
        """
        if self._sftp is None:
            self._sftp = paramiko.SFTPClient.from_transport(self._transport)
        self._sftp.put(localfilepath, remotefilepath)
        return

    def DownloadFolder(self, remotedir, localdir):
        """
        下载远程文件夹
        :param remotedir: 远程目录
        :param localdir:  本地保存路径
        :return: 
        """
        remotefolderpathobject = pathlib.PurePosixPath(remotedir)
        foldername = remotefolderpathobject.name
        localfolderpathobject = pathlib.PureWindowsPath(localdir, foldername)
        localfolderpath = str(localfolderpathobject)
        self.Download(remotedir, localfolderpath)
        return

    def UploadFolder(self, localfolderpath, remotedir):
        """
        上传文件夹
        :return:
        """
        localfolderpathobject = pathlib.PurePath(localfolderpath)
        foldername = localfolderpathobject.name
        localzipfilepath = UFP.ZipFile(localfolderpath)
        localzipfilepathobject = pathlib.PurePath(localzipfilepath)
        zipfilename = localzipfilepathobject.name
        remotezipfilepathobject = pathlib.PurePosixPath(remotedir, zipfilename)
        remotezipfilepath = str(remotezipfilepathobject)
        self.Upload(localzipfilepath, remotezipfilepath)
        os.remove(remotezipfilepath)
        return

    def exec_command(self, command):
        """
        执行命令
        :param command: 执行的命令语句
        :return:
        """
        if self._client is None:
            self._client = paramiko.SSHClient()
            knowremoteserver = paramiko.AutoAddPolicy()
            self._client.set_missing_host_key_policy(knowremoteserver)
            self._client._transport = self._transport
        stdin, stdout, stderr = self._client.exec_command(command)
        data = stdout.read().decode('utf-8')
        if len(data) > 0:
            # print(data.strip())  # 打印正确结果
            return data
        err = stderr.read()
        if len(err) > 0:
            # print(err.strip())   # 输出错误结果
            return err

    def close(self):
        """
        关闭链接
        :return:
        """
        if self._transport:
            self._transport.close()
        if self._client:
            self._client.close()
        return
